<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// check if the user is logged-in
include 'authentication.php';
isAuthorized();

$parent_dir = $_GET['parent_dir'];
$idf = $_GET['idf'];

include_once($_SERVER["DOCUMENT_ROOT"]."/web-doc/protected/database/MySqlDAO.php");
include_once("../utils/redirect.php");
include_once('../utils/directories.php');

$mySqlDAO = new MySqlDAO();
                        
if ($parent_dir) {
    if (!empty($_POST['isToInsert'])) {
        if (!empty($_POST['title'])) {

            $title = $_POST['title'];

            /* Check if title is already used */
            $idfres = $mySqlDAO->checkFileTitle($parent_dir, $title);
            if (!empty($idfres) && $idf != $idfres) {
                echo "ERROR: Il nome <b>$title</b> è già usato per un'altro file in questa categoria. Scegliere un'altro nome.<br>";
            } else {
                if ($_FILES['userfile']['size'] > 0) {

                    $fileName = $_FILES['userfile']['name'];
                    $tmpName = $_FILES['userfile']['tmp_name'];
                    $fileSize = $_FILES['userfile']['size'];
                    $fileType = $_FILES['userfile']['type'];

                    if (!get_magic_quotes_gpc()) {
                        $fileName = addslashes($fileName);
                    }

                    /* Check if file is already uploaded */
                    $existTitle = $mySqlDAO->checkFileExist($parent_dir, $fileName);
                    if (!empty($existTitle)) {
                        echo "ERROR: Il file <b>$fileName</b> esiste già in questa categoria con titolo <b>$existTitle</b>.<br>";
                    } else {

                        $dbPath = $mySqlDAO->getFilePath($idf);
                        $oldname = $mySqlDAO->getFileName($idf);
                        $oldPath = getRootDir() . $dbPath . $oldname;
                        $newPath = getRootDir() . $dbPath . $fileName;

                        if (@is_uploaded_file($tmpName)) {

                            @move_uploaded_file($tmpName, $newPath)
                                    or die("Impossibile spostare il file, controlla l'esistenza o i permessi della directory dove fare l'upload.");

                            unlink($oldPath) or die("ERROR: Cannot delete file " . $oldPath);
                        } else
                            die("ERROR: Cannot upload file " . $fileName);
                        
                        $mySqlDAO->modifyFile($idf, $title, $dbPath, $fileName, $fileSize, $fileType, $parent_dir);
                        redirect("showcategory.php?idc=$parent_dir",FALSE);
                    }
                }
                else {
                    $mySqlDAO->modifyFile($idf, $title, NULL, NULL, NULL, NULL, NULL);
                    redirect("showcategory.php?idc=$parent_dir",FALSE);
                }
            }
        } else {
            echo("ATTENZIONE: Il titolo del file non può essere vuoto.");
        }
    }
} else {
    die("Parent directory mancante. <a href=javascript:history.back(-1)>Indietro</a>");
}

$title = $mySqlDAO->getFileTitle($idf);
?>


<html>
    <head>
        <title>webDoc - Modify file</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">
    </head>
    <body>
<?php
$formaction = "modifyfile.php?parent_dir=$parent_dir&idf=$idf";
?>
        <form method="post" enctype="multipart/form-data" action="<?php echo $formaction ?>">
            <table width="450" border="0" cellpadding="1" cellspacing="1" class="box">
                <tr>
                    <td valign="top" colspan="2">
                        Titolo <input type="text" name="title" size="50" maxlength="255" value="<?php echo $title ?>">
                    </td>
                </tr>
                <tr>
                    <td width="246">
                        <input type="hidden" name="isToInsert" value="true">
                        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
                        <input name="userfile" type="file" id="userfile">
                    </td>
                </tr>
                <tr>
                    <td width="80" colspan="2">
                        <br><br>
                        <input type="submit" value="Modifica" />
                        <!--<input name="upload" type="submit" class="box" id="upload" value=" Upload ">-->
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>